package com.xf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xf.common.core.domain.DmpResult;
import com.xf.common.core.entity.dmp.DmpAuthResource;
import com.xf.common.core.entity.dmp.DmpAuthRole;
import com.xf.common.core.page.PageBuilder;
import com.xf.common.core.page.PageQuery;
import com.xf.service.DmpAuthResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author JoeyLiu
 * @create 2022-04-25 16:13
 */

@RestController
@RequestMapping("/resource")
@Api(tags = "资源管理")
@Slf4j
@Validated
public class DmpAuthResourceController {
    @Resource
    private DmpAuthResourceService dmpAuthResourceService;

    @ApiOperation("批量添加角色资源")
    @PostMapping(value = "/addBatchRoleResource")
    public DmpResult batchAddRoleResource(@RequestBody List<DmpAuthRole> dmpAuthRoles) {
        dmpAuthResourceService.batchAddRoleResource(dmpAuthRoles);
        return DmpResult.success();
    }

    @ApiOperation("根据角色查询资源")
    @PostMapping(value = "/getByRoleIds")
    public DmpResult queryResourceByRoleIds(@RequestBody List<Long> roleIds) {
        return DmpResult.data(dmpAuthResourceService.queryResourceByRoleIds(roleIds));
    }

    @ApiOperation("添加资源")
    @PostMapping(value = "/add")
    public DmpResult add(@Valid @RequestBody DmpAuthResource dmpAuthResource) {
        return dmpAuthResourceService.insert(dmpAuthResource);
    }

    @ApiOperation("删除资源")
    @PostMapping(value = "/delete")
    public DmpResult delete(@RequestBody DmpAuthResource dmpAuthResource) {
        return dmpAuthResourceService.removeByResourceId(dmpAuthResource.getId());
    }

    @ApiOperation("查询资源")
    @PostMapping(value = "/getList")
    public DmpResult list() {
        return DmpResult.data(dmpAuthResourceService.list());
    }


    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "资源名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "url", value = "资源路径", paramType = "query", dataType = "String"),
    })
    @GetMapping(value = "/getPage")
    public DmpResult<IPage<DmpAuthResource>> page(PageQuery pageQuery, String name, String url ) {
        return DmpResult.data(dmpAuthResourceService.page(PageBuilder.build(pageQuery), name, url ));
    }

    @ApiOperation("修改资源")
    @PostMapping(value = "/update")
    public DmpResult update(@RequestBody DmpAuthResource dmpAuthResource) {
        return dmpAuthResourceService.updateByResourceId(dmpAuthResource);
    }


}
